<?php
/**
 * @version		$Id: controller.php 11299 2008-11-22 01:40:44Z ian $
 * @package		Joomla
 * @subpackage	Users
 * @copyright	Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * See COPYRIGHT.php for copyright notices and details.
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.controller');

/**
 * Users Component Controller
 *
 * @package		Joomla
 * @subpackage	Users
 * @since 1.5
 */
class BanwordsController extends JController
{
	/**
	 * Constructor
	 *
	 * @params	array	Controller configuration array
	 */
	function __construct($config = array())
	{
		parent::__construct($config);

		// Register Extra tasks//注册任务与方法的关联
		$this->registerTask( 'add'  , 	'display'  );
//		$this->registerTask( 'edit'  , 	'display'  );
		$this->registerTask( 'apply', 	'save'  );
		$this->registerTask( 'check', 	'check'  );
		//$this->registerTask( 'flogout', 'logout');
		//$this->registerTask( 'unblock', 'block' );
//		$this->registerTask( 'enban', 'enban');
		
	}

	/**
	 * Displays a view
	 */
	function display( )
	{
		switch($this->getTask())
		{
			case 'add'     :
			{	JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'layout', 'form'  );
				JRequest::setVar( 'view', 'banword' );
				JRequest::setVar( 'edit', false );
			} break;
			case 'checkbanword'    :
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'layout', 'form'  );//设置layout的值，form 或fault；
				JRequest::setVar( 'view', 'checkbanword' );//
				//JRequest::setVar( 'edit', true );
			} break;
			case 'library'    :
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'layout', 'form'  );
				JRequest::setVar( 'view', 'library' );//设置view的参数，
				//JRequest::setVar( 'edit', true );
			} break;
		}

		parent::display();
	}

	/**
	 * Saves the record
	 */
	function save()
	{
		global $mainframe;

		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$option = JRequest::getCmd( 'option');

		// Initialize some variables
		$db			= & JFactory::getDBO('shaishaidb');
//		$me			= & JFactory::getUser();//获取当前用户
//		$acl			=& JFactory::getACL();//获取授权
//		$MailFrom	= $mainframe->getCfg('mailfrom');
//		$FromName	= $mainframe->getCfg('fromname');
//		$SiteName	= $mainframe->getCfg('sitename');

 		// Create a new JUser object
//		$user = new JUser(JRequest::getVar( 'id', 0, 'post', 'int'));

//		$original_gid = $user->get('gid'); 
		$post = JRequest::get('post');

		$query = "INSERT INTO banwords SET banword = '".$post['banword']."'";
		
		//echo $query;
		$db->setQuery( $query );			
		$db->loadResult();
		
		switch ( $this->getTask() )
		{
			case 'apply':
				$msg = JText::_( '非法关键字添加成功 ' );
				
				$this->setRedirect( 'index.php?option=com_banwords&view=banword&task=edit&cid[]='. $post['id'], $msg );

				break;

			case 'save':
			default:
				$msg = JText::_( '非法关键字添加成功 ');
				$this->setRedirect( 'index.php?option=com_banwords', $msg );
				break;
		}
	}
	function check() 
	{
		$post = JRequest::get('post');
		$string = $post['content'];
		
		$fileName=JPATH_BASE.DS.'components'.DS.'com_banwords'.DS.'banwords.txt';
		
			if ( !($words = file_get_contents( $fileName )) ){
				$msg = JText::_( '打开非法关键字文件失败！ ');
				$this->setRedirect( 'index.php?option=com_banwords', $msg );
			}
			$string = strtolower($string);
			$matched = preg_match('/'.$words.'/i', $string, $result);
			if ( $matched && isset($result[0]) && strlen($result[0]) > 0 )
			{
				if ( strlen($result[0]) == 2 ){
					$matched = preg_match('/'.$words.'/iu', $string, $result);
				}	
				if ( $matched && isset($result[0]) && strlen($result[0]) > 0 )	{
					$msg = JText::_( '该内容非法！ ' );
				    $this->setRedirect( 'index.php?option=com_banwords&view=checkbanword&task=checkbanword', $msg );
					
				}else{
					$msg = JText::_( '该内容合法！ ');
				    $this->setRedirect( 'index.php?option=com_banwords&view=checkbanword&task=checkbanword', $msg );
				}		
			}else{
				$msg = JText::_( '该内容合法！ ');
				$this->setRedirect( 'index.php?option=com_banwords&view=checkbanword&task=checkbanword', $msg );
		}
	}
		
    function remove()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$db 			=& JFactory::getDBO('shaishaidb');
		$currentUser 	=& JFactory::getUser();
		$acl			=& JFactory::getACL();
		$cid 			= JRequest::getVar( 'cid', array(), '', 'array' );

		JArrayHelper::toInteger( $cid );

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( '请选择非法关键字删除！', true ) );
		}

		foreach ($cid as $id)
		{
			// check for a super admin ... can't delete them
			$query = 'DELETE FROM banwords where id = '.$id;
			$db->setQuery( $query );
			if($db->loadResult()) $msg = '删除非法关键字失败！';
			else $msg = '删除非法关键字成功！';
			
		}		
		$this->setRedirect( 'index.php?option=com_banwords', $msg);
		
	}
	
	function cancel( )
	{
		$this->setRedirect( 'index.php?option=com_banwords' );
	}


}